package com.mocha.bass.test.jiangan;

import com.alibaba.fastjson.JSON;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.vehicle.dmo.PointBO;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePointPGEntity;
import com.hyzh.latte.vehicle.repository.CameraPGRepository;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import com.hyzh.latte.vehicle.repository.PolePGRepository;
import com.hyzh.latte.vehicle.repository.PolePointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/29 23:36
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class PolePointTimeUpdate {

    @Autowired
    private PointPGRepository pointPGRepository;
    @Autowired
    private PolePointPGRepository polePointPGRepository;
    @Autowired
    private CameraPGRepository cameraPGRepository;
    @Autowired
    private PolePGRepository polePGRepository;
    //key 点号
    private final static Map<String, PointPGEntity> pointMap = new HashMap<>();
    //key： x+"_"+y
    private final static Map<String, PointPGEntity> coordMap = new HashMap<>();

    private final static Map<String, PolePGEntity> poleMap = new HashMap<>();


    @Test
    public void poleEdge(){
        List<PointPGEntity> pointList = pointPGRepository.lambdaQuery().eq(PointPGEntity::getParkCode, "jiangan").list();
        Map<String, PointPGEntity> pointMap = pointList.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));

        List<PolePointPGEntity> list = polePointPGRepository.lambdaQuery().like(PolePointPGEntity::getSource, "pole76").like(PolePointPGEntity::getTarget, "pole77").list();
        for (PolePointPGEntity entity : list){
            doUpdatePolePoint(entity, pointMap);
        }

    }


    public void doUpdatePolePoint(PolePointPGEntity entity,   Map<String, PointPGEntity> pom){

        PolePointPGEntity pgEntity = new PolePointPGEntity();
        pgEntity.setId(entity.getId());
        String json = pointJSON(pom, entity.getPoints());
        pgEntity.setPointJson(json);

        polePointPGRepository.updateById(pgEntity);

    }

    public static String pointJSON(Map<String, PointPGEntity> pom, String points){
        String[] ss = points.split(",");
        List<PointBO> pointBOList = new ArrayList<>(ss.length);
        for (String s : ss){
            PointPGEntity entity = pom.get(s);
            PointBO pointBO = new PointBO();
            pointBO.setLon(entity.getLon());
            pointBO.setLat(entity.getLat());
            pointBO.setH(entity.getHeight());
            pointBO.setT(1);
            pointBO.setDistance(50);
            pointBOList.add(pointBO);
        }


        return JSON.toJSONString(pointBOList);
    }



}
